cmake_minimum_required(VERSION 3.10)
project(kae_provider C)

set(KAEP_LIB_DIR "" CACHE PATH "Custom library search path")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
option(ENABLE_ASAN "Enable AddressSanitizer" OFF)

# Debug and Release mode config.
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -O0 -fPIC -shared")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native -fPIC -shared")

# ASAN Config
if(ENABLE_ASAN)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
endif()


# Set source files
set(PROVIDER_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/cipher/kaev1_modes.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/digests/kaev1_digests.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/dh/src/kaev1_dh_keygen.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/dh/src/kaev1_dh.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/dh/src/kaev1_dh_para.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/rsa/src/kaev1_rsa.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/rsa/src/kaev1_rsa_ctrl.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/rsa/src/kaev1_rsa_keygen.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/rsa/src/kaev1_rsa_padding.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_cipher.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_keyexch.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_keymgmt.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_md.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_pkey.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_provider.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_sign.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider/kaev1_decode.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/util/kaev1_mem.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/codecskey/src/kaev1_decode_der2key.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/codecskey/src/kaev1_decode_rsa.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/codecskey/src/kaev1_encode_decode_utils.c
)

# link dir
if(KAEP_LIB_DIR)
    link_directories(${KAEP_LIB_DIR})
endif()

if(CUSTOM_CFLAGS)
    message(STATUS "CUSTOM_CFLAGS: ${CUSTOM_CFLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CUSTOM_CFLAGS}")
endif()

# Create shared library
add_library(${PROJECT_NAME} SHARED ${PROVIDER_SOURCES})

target_include_directories(${PROJECT_NAME}
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/include
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src/include

        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/cipher
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/digests
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/dh/include
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/alg/rsa/include
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/util
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/provider
        ${CMAKE_CURRENT_SOURCE_DIR}/src/kaev1/codecskey/include

        # OpenHitls
        ${OPENHITLS_INCLUDE_DIRS}
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/include/crypto
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/include/bsl
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/include/tls
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/bsl/asn1/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/bsl/err/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/bsl/obj/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/config/macro_config
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/bn/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/codecskey/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/dh/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/eal/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/eal/src
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/provider/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/crypto/rsa/include
        ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/platform/Secure_C/include

        # WD Driver
        ${WD_DIR}
        ${WD_DIR}/include
)

target_link_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/build
    ${CMAKE_CURRENT_SOURCE_DIR}/platform/openhitls/platform/Secure_C/lib
)

message(STATUS "WD_DIR: ${WD_DIR}")
message(STATUS "hitls_crypto: ${HITLS_CRYPTO_LIB}")
message(STATUS "hitls_bsl: ${HITLS_BSL_LIB}")
message(STATUS "boundscheck: ${SECUREC_LIB}")

target_link_libraries(${PROJECT_NAME}
    hitls_bsl
    hitls_crypto
    boundscheck
    wd
)